package com.itheima.controller;


import com.itheima.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/teachers")
public class TeacherController {


    @Autowired
    private TeacherService teacherService;


    /**
     * 查看课程评论
     * @param page
     * @param pageSize
     * @param name 姓名
     * @param gender 性别
     * @param begin 课程评论开始时间
     * @param end 课程评论结束时间
     * @return
     */
    @GetMapping("/discuss")
    public Result discuss(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize,
                          String name,Integer gender,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end
                          ){
        log.info("分页查询 {} {} {} {} {}",page,pageSize,gender,begin,end);
        PageBean contentPage = teacherService.getDiscuss(page,pageSize,name,gender,begin,end);


        return Result.success(contentPage);
    }

    /**
     * 查询教师课程
     * @param page
     * @param pageSize
     * @param name 课程名称
     * @return
     */
    @GetMapping("/course")
    public Result course(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize,
            String name) {
        log.info("分页查询教师课程信息{}",name);

        PageBean pageBean = teacherService.getCourse(page,pageSize,name);

        return Result.success(pageBean);
    }

    /**
     * 添加课程
     * @param course
     * @return
     */
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        log.info("添加课程:{}",course);
        //添加课程
        teacherService.insertCourse(course);
        return Result.success();
    }

    /**
     * 根据课程id修改课程名称 简介 所属分类
     * @param course
     * @return
     */
    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        log.info("修改课程信息{}",course);
        teacherService.updateCourse(course);
        return Result.success();
    }

    /**
     * 根据分类id删除课程信息
     * @param id 分类id
     * @return
     */
    @DeleteMapping
    public Result deleteCourse(Integer id){
        log.info("根据分类id删除课程信息:{}",id);
        teacherService.deleteCourses(id);
        return Result.success();
    }

    /**
     * 查询教师个人信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getTeacherById(@PathVariable Integer id) {
       Teacher teacher = teacherService.getTeacherById(id);
        return Result.success(teacher);
    }

    /**
     * 更新教师个人信息
     * @param teacher
     */
    @PutMapping("/updateUserMsg")
    public Result updateUserMsg(@RequestBody Teacher teacher){
        log.info("更新教师个人信息{}",teacher);
        teacherService.updateUserMsg(teacher);
        return Result.success();
    }


}
